package com.maodou.android.ui.page.splash

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.maodou.android.R
import com.maodou.android.ui.page.WebViewKey
import com.maodou.android.viewmodel.splash.SplashViewModel
import com.maodou.core.datastore.DataStore
import com.maodou.core.navigation.Navigator3
import com.maodou.core.ui.components.VSpacer
import com.maodou.core.ui.components.debouncedClickable
import com.maodou.core.ui.theme.AdaptiveDimensions
import com.maodou.core.ui.theme.CompactTypography
import com.maodou.core.ui.theme.ComponentSizes
import com.maodou.core.ui.theme.FontSizes
import com.maodou.core.ui.theme.Padding
import com.maodou.core.ui.theme.Spacing
import com.maodou.core.ui.theme.current
import kotlin.system.exitProcess

/**
 * @Author Xing
 * @CreateTime 2025年08月27日
 * @Description TODO
 */
@Composable
fun PrivacyPolicyUI(
    viewModel: SplashViewModel? = null
) {
    val adaptiveDimensions = AdaptiveDimensions.current()

    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(Spacing.ExtraHuge),
        shape = RoundedCornerShape(adaptiveDimensions.medium),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surface
        ),
        elevation = CardDefaults.cardElevation(
            defaultElevation = 2.dp
        )
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(adaptiveDimensions.medium),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {

            Text(
                text = "欢迎使用「${stringResource(R.string.app_name)}」",
                color = MaterialTheme.colorScheme.onSurface,
                fontWeight = FontWeight.W600,
                style = CompactTypography.titleMedium,
                modifier = Modifier.padding(adaptiveDimensions.large),
            )

            PrivacyPolicyText(Modifier)

            VSpacer(10.dp)

            Column (
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(vertical = adaptiveDimensions.medium, horizontal = adaptiveDimensions.large),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {

                Box(
                    contentAlignment = Alignment.Center,
                    modifier = Modifier
                        .height(ComponentSizes.ButtonHeightLarge)
                        .fillMaxWidth()
                        .background(
                            color = MaterialTheme.colorScheme.primary,
                            RoundedCornerShape(adaptiveDimensions.large)
                        )
                        .debouncedClickable {
                            viewModel?.setAgreementState(true)
                        }
                ) {
                    Text(
                        "同意",
                        style = MaterialTheme.typography.bodyMedium,
                        color = Color.White
                    )
                }

                Spacer(modifier = Modifier.width(20.dp))

                Box(
                    contentAlignment = Alignment.Center,
                    modifier = Modifier
                        .height(ComponentSizes.ButtonHeightLarge)
                        .fillMaxWidth()
                        .debouncedClickable {
                            exitProcess(0)
                        }
                ) {
                    Text(
                        "退出",
                        style = MaterialTheme.typography.bodyMedium,
                        color = Color.Black
                    )
                }

            }

        }
    }

}

@Composable
private fun PrivacyPolicyText(modifier: Modifier) {
    val annotatedString = buildAnnotatedString {
        append("\u3000\u3000感谢您信任并使用${stringResource(R.string.app_name)}的产品和服务我们依据最新的法律法规、监管政策要求，制定了独立的")

        // 隐私协议
        pushStringAnnotation(
            tag = "p1",
            annotation = "1"
        )
        withStyle(
            style = SpanStyle(
                fontWeight = FontWeight.W600,
                textDecoration = TextDecoration.Underline
            )
        ) {
            append("隐私政策")
        }
        pop()

        append("，特向您推送本提示。请您仔细阅读并充分理解相关条款。\n\u3000\u3000您点击“同意”，即表示您已阅读并同意上述协议条款，我们尽全力保障您的合法权益并继续为您提供优质的产品和服务。\n\u3000\u3000如您点击“不同意”，将可能导致无法继续使用我们的产品和服务。您可通过阅读完整版")

        // 隐私政策
        pushStringAnnotation(
            tag = "p2",
            annotation = "2"
        )
        withStyle(
            style = SpanStyle(
                fontWeight = FontWeight.W600,
                textDecoration = TextDecoration.Underline
            )
        ) {
            append("隐私政策")
        }
        pop()

        append("了解详尽的条款内容。")
    }

    ClickableText(
        text = annotatedString,
        style = CompactTypography.bodyMedium.copy(
            lineHeight = FontSizes.TitleMedium,
            fontSize = FontSizes.Body
        ),
        onClick = { offset ->
            annotatedString.getStringAnnotations(start = offset, end = offset)
                .firstOrNull()?.let { annotation ->
                    Navigator3.navigate(WebViewKey("https://www.baidu.com/"))
                }
        },
        modifier = modifier.padding(horizontal = Padding.Large)
    )
}

@Preview
@Composable
fun PrivacyPolicyPreview() {
    PrivacyPolicyUI()
}